package com.government.web.controller.article;

import java.util.List;
import cn.hutool.core.util.StrUtil;
import com.government.app.vo.KeywordSearchVO;
import com.government.article.bo.ArticleBO;
import com.government.article.bo.ArticleInformationBO;
import com.government.article.bo.InformationCollectBO;
import com.government.article.service.IArticleInformationService;
import com.government.article.vo.ArticleInformationVO;
import com.government.article.vo.InformationCollectDetailVO;
import com.government.common.annotation.Anonymous;
import com.government.common.annotation.RepeatSubmit;
import com.government.common.exception.ServiceException;
import com.government.member.bo.CancelFavoritesBO;
import com.government.member.domain.UserHobby;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.government.common.annotation.Log;
import com.government.common.core.controller.BaseController;
import com.government.common.core.domain.AjaxResult;
import com.government.common.enums.BusinessType;
import com.government.article.service.IArticleService;
import com.government.common.core.page.TableDataInfo;

/**
 * 文章Controller
 *
 * @author system
 * @date 2023-09-20
 */
@RestController
@RequestMapping("/article/article")
public class ArticleController extends BaseController
{
    @Autowired
    private IArticleService articleService;
    @Autowired
    private IArticleInformationService articleInformationService;

    /**
     * 查询文章列表
     */
    @PreAuthorize("@ss.hasPermi('article:article:list')")
    @GetMapping("/list")
    public TableDataInfo list(ArticleInformationBO bo)
    {

        List<ArticleInformationVO> list = articleService.selectArticleList(bo);
        return getDataTable(list);
    }

//    /**
//     * 导出文章列表
//     */
//    @PreAuthorize("@ss.hasPermi('article:article:export')")
//    @Log(title = "文章", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, Article article)
//    {
//        List<Article> list = articleService.selectArticleList(article);
//        ExcelUtil<Article> util = new ExcelUtil<Article>(Article.class);
//        util.exportExcel(response, list, "文章数据");
//    }

    /**
     * 获取文章详细信息
     */
    @PreAuthorize("@ss.hasPermi('article:article:query')")
    @GetMapping(value = "/{articleId}")
    public AjaxResult getInfo(@PathVariable("articleId") Long articleId)
    {
        return success(articleService.selectArticleByArticleId(articleId));
    }

    /**
     * 新增文章
     */
    @RepeatSubmit
    @PreAuthorize("@ss.hasPermi('article:article:add')")
    @Log(title = "文章", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ArticleBO bo)
    {
        return toAjax(articleService.insertArticle(bo));
    }

    /**
     * 修改文章
     */
    @RepeatSubmit
    @PreAuthorize("@ss.hasPermi('article:article:edit')")
    @Log(title = "文章", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ArticleBO bo)
    {
        return toAjax(articleService.updateArticle(bo));
    }

    /**
     * 删除文章
     */
    @RepeatSubmit
    @PreAuthorize("@ss.hasPermi('article:article:remove')")
    @Log(title = "文章", businessType = BusinessType.DELETE)
	@DeleteMapping("/{articleId}")
    public AjaxResult remove(@PathVariable Long[] articleId)
    {
        articleService.deleteArticleByArticleId(articleId);
        return toAjax(true);
    }

    @PostMapping("/articleDataHandler")
    public void articleDataHandler()
    {
        articleService.articleDataHandler();
    }

    @PostMapping("/articlePolicyRelHandler")
    public void articlePolicyRelHandler()
    {
        articleService.articlePolicyRelHandler();
    }


    /**
     * 点赞收藏
     * @param userHobby
     */
    @PostMapping("/favorites")
    public AjaxResult favorites(UserHobby userHobby)
    {
        articleService.favorites(userHobby);
        return success();
    }

    /**
     * 批量关注
     * @param
     */
    @PostMapping(value = "/batchFollow", produces = "application/json;charset=UTF-8")
    public AjaxResult batchFollow(@RequestBody CancelFavoritesBO bo)
    {
        articleService.batchFollow(bo.getCollectIds());
        return success();
    }

    /**
     * 批量取消收藏
     * @param
     */
    @PostMapping(value = "/cancelFavorites", produces = "application/json;charset=UTF-8")
    public AjaxResult cancelFavorites(@RequestBody CancelFavoritesBO bo)
    {
        articleService.cancelFavorites(bo.getCollectIds());
        return success();
    }

    /**
     * 根据条件查询资讯汇总
     * @param
     */
    @PostMapping(value = "/getInformationCollect")
    public TableDataInfo getInformationCollect(@RequestBody InformationCollectBO bo)
    {
        ServiceException.throwExOnCondition(StrUtil.isBlank(bo.getPolicyId())&&StrUtil.isBlank(bo.getOrgId()),"部门id或者政策id不能为空");

        List<InformationCollectDetailVO> informationCollectDetailVOList = articleService.getInformationCollect(bo);
        return getDataTable(informationCollectDetailVOList);
    }

    /**
     * 获取热点资讯
     */
    @Anonymous
    @GetMapping(value = "/getHotInformation")
    public AjaxResult getHotInformation(int type)
    {
        return success(articleInformationService.getHotInformation(type));
    }

    /**
     * 关键字搜索列表（分页）
     */
    @PostMapping("/getInterestInformationList")
    public TableDataInfo getInterestInformationList() {

        List<KeywordSearchVO> result =articleInformationService.selectInterestInformationList();
        return getDataTable(result);
    }

}
